package org.alvin.gencode;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Lists;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 表单转换
 */
public class FormTransferService {

    static List<String> tagNames = Lists.newArrayList("Affix", "Alert", "Anchor", "AnchorLink", "AutoComplete", "Avatar", "Badge", "Breadcrumb", "BreadcrumbItem", "Button", "ButtonGroup", "Card", "Carousel", "CarouselItem", "Cascader", "Cell", "CellGroup", "Checkbox", "CheckboxGroup", "Circle", "Circle", "as", "ICircle", "Collapse", "CollapsePanel", "ColorPicker", "Content", "DatePicker", "DatePickerOptions", "Divider", "Drawer", "Dropdown", "DropdownMenu", "DropdownItem", "Footer", "Form", "FormItem", "Row", "Col", "Header", "Icon", "Input", "InputNumber", "Scroll", "Split", "Layout", "LoadingBar", "LoadingBarConfig", "Menu", "MenuGroup", "MenuItem", "Submenu", "Message", "MessageConfig", "Modal", "ModalInstance", "ModalConfig", "Notice", "NoticeConfig", "NoticeGlobalConfig", "Page", "Poptip", "Progress", "Radio", "RadioGroup", "Rate", "Select", "Option", "OptionGroup", "Sider", "Slider", "Spin", "Steps", "StepsStep", "Switch", "Switch", "as", "ISwitch", "Table", "TableColumn", "TableRenderCreateElementData", "TableColumnRenderParams", "TableColumnRenderHeadParams", "TableExportCsvParams", "Tabs", "TabPane", "Tag", "Time", "Timeline", "TimelineItem", "TimePicker", "Tooltip", "Transfer", "Tree", "TreeChild", "Upload");

    public String transfer(String src) {
        for (String tag : tagNames) {
            if (tag.equals("Form")) {
                continue;
            }
            String startTag = "[<]" + tag;
            String tagName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, tag).replaceAll("\\_", "-");
            src = src.replaceAll(startTag, "<el-" + tagName);
            String endTag = "[<][/]" + tag ;
            src = src.replaceAll(endTag, "</el-" + tagName );
        }
        return src;
    }

//    String transfer(String src) throws DocumentException {
//        StringBuilder srcBuilder = new StringBuilder();
//        //这是固定，不做兼容处理
//        srcBuilder.append("<root>");
//        srcBuilder.append(src);
//        srcBuilder.append("</root>");
//
//        SAXReader saxReader = new SAXReader();
//        Document document = saxReader.read(new ByteArrayInputStream(srcBuilder.toString().getBytes()));
//        List<Element> eles = document.getRootElement().elements("FormItem");
//
//        Document distDoc = DocumentHelper.createDocument();
//        for (Element el : eles) {
//            String tagName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, el.getName().toString()).replaceAll("_", "-");
//            Element element = distDoc.addElement(tagName);
//            for (Attribute attribute : el.attributes()) {
//                element.addAttribute(attribute.getQName(), attribute.getValue());
//            }
//        }
//        return xmlPretty(document);
//    }
//
//    private String xmlPretty(Document document) {
//        OutputFormat format = OutputFormat.createPrettyPrint();
//        format.setIndent("    ");
//        format.setEncoding(document.getXMLEncoding());
//        try {
//            StringWriter out = new StringWriter();
//            XMLWriter writer = new XMLWriter(out, format);
//            writer.write(document);
//            writer.flush();
//            return out.toString();
//        } catch (IOException e) {
//            String xml = document.asXML();
//            return xml;
//        }
//    }
//
//    public static boolean isLowerCamelCase(String name){
//        String regex = "^[a-z]+([A-Z][a-z]*)*$";
//        return name.matches(regex);
//    }
//    public static boolean isUpperCamelCase(String name){
//        String regex = "^[A-Z]([A-Z][a-z]*)*$";
//        return name.matches(regex);
//    }


    public static void main(String[] args) throws DocumentException {
        String text ="<Button @click=\"doAdd()\" :title=\"$t('CarChargepileList.tianjia')\" type=\"primary\"\n" +
                "              v-permission=\"'carChargepile:add'\">{{ $t(\"CarChargepileList.tianjia\") }}</Button>\n" +
                "            <Button @click=\"doTransfer()\" v-permission=\"'carChargepile:transfer'\">{{\n" +
                "          $t(\"CarChargepileList.piliangzhuangzhuanyi\")\n" +
                "        }}</Button>\n" +
                "            <Button @click=\"doBatchRate()\" v-permission=\"'carChargepile:settingsRate'\">{{\n" +
                "          $t(\"CarChargepileList.piliangxiugaifeilu\")\n" +
                "        }}</Button>\n" +
                "            <Dropdown @on-click=\"doMoreOptTool($event)\">\n" +
                "              <Button>\n" +
                "                更多操作\n" +
                "                <Icon type=\"ios-arrow-down\"></Icon>\n" +
                "              </Button>\n" +
                "              <DropdownMenu slot=\"list\">\n" +
                "                <DropdownItem name=\"doCreateDownloadTask\" v-permission=\"'carChargepile:export'\">导出</DropdownItem>\n" +
                "                <DropdownItem name=\"toShowDownloadTask\" v-permission=\"'carChargepile:export'\">导出记录</DropdownItem>\n" +
                "                <DropdownItem name=\"doQrCode\" v-permission=\"'carChargepile:settingsQrCode'\">{{\n" +
                "          $t(\"CarChargepileList.piliangshezhierweima\")\n" +
                "        }}</DropdownItem>\n" +
                "                <DropdownItem name=\"downloadQrCode\" v-permission=\"'carChargepile:downloadQrCode'\">{{\n" +
                "          $t(\"CarChargepileList.xiazaierweima\")\n" +
                "        }}</DropdownItem>\n" +
                "                <DropdownItem name=\"doShowTitltDialog\" v-permission=\"'carChargepile:downloadCustomTitleQrCode'\">{{\n" +
                "          $t(\"CarChargepileList.xiazaierweimadingzhibiaoti\")\n" +
                "        }}</DropdownItem>\n" +
                "                <DropdownItem name=\"doBatchUpgrade\" v-permission=\"'carChargepile:batchUpgrade'\">{{\n" +
                "          $t(\"CarChargepileList.piliangshengji\")\n" +
                "        }}</DropdownItem>\n" +
                "              </DropdownMenu>\n" +
                "            </Dropdown>";
        System.out.println(new FormTransferService().transfer(text));

    }
}
